﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity.Car_Rental;
using Data.Car_Rental;
using Entity.CommonEnums;
using Web.Commons;
using Car_Rental.Controllers.Helpers;
using Web.Commons.Helpers;
using Entity;

namespace Car_Rental.Controllers
{
    /// <summary>
    /// 系统公用页面的Controller
    /// </summary>
    public class UCController : BaseController
    {
        #region Constructs
        IBlockRepository bLockRepository = null;
        List<Block> blockList = BlockRepository.blockList;
        IUserRepository iUserRepository = null;
        INewsRepository iNewsRepository = null;
        IRentalRecordRepository iRentalRecordRepository = null;
        IHelperCenterCategoryRepository iHelperCenterCategoryRepository = null;
        public UCController()
        {
            bLockRepository = new BlockRepository();
            iUserRepository = new UserRepository();
            iNewsRepository = new NewsRepository();
            iRentalRecordRepository = new RentalRecordRepository();
            iHelperCenterCategoryRepository = new HelperCenterCategoryRepository();
        }
        #endregion

        #region 页面模块

        public ActionResult Foot()
        {
            List<Block> model = blockList.Where(i => ((BlockType)i.Type & BlockType.Foot) != 0).ToList();
            return View(model);
        }
        public ActionResult Left()
        {
            List<Block> model = blockList.Where(i => ((BlockType)i.Type & BlockType.Left) != 0).ToList();
            ViewData["News"] = iNewsRepository.GetModelForSite().Take(20).ToList();
            return View(model);
        }
        public ActionResult Right()
        {
            return View();
        }

        public ActionResult Top()
        {

            return View(iHelperCenterCategoryRepository.GetModel().Where(i => i.Name != "根").OrderBy(i => i.SortNumber).ToList());
        }
        #endregion

        #region 登陆模块
        public PartialViewResult LogOn()
        {
            return PartialView();
        }
        [HttpPost]
        public PartialViewResult LogOn(UserBases entity)
        {
            CommonMethod.UserLogOnModule(this, entity, "/UC/LogOn");
            return PartialView(entity);
        }
        public void LogOut()
        {
            CommonMethod.UserLogOutModule(this);
        }
        #endregion

        #region 在线订车模块
        public ActionResult Rental()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Rental(RentalRecord entity)
        {
            if (Request.FilePath == "/UC/Rental") //判断是否从指定地址提交过来
            {
                if (entity.IsValid)
                {
                    if (iRentalRecordRepository.RentalCar(entity).IsComplete)
                    {
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "订车失败");
                    }
                }
                else
                {
                    entity.GetRuleViolations().ToList().ForEach(i =>
                    {
                        ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                    });
                }
            }
            return View(entity);
        }
        #endregion



    }
}
